package com.neusoft.shopingapi.controller;

import com.neusoft.shopingapi.comm.Page;
import com.neusoft.shopingapi.entity.GoodsEntity;
import com.neusoft.shopingapi.entity.TypeEntity;
import com.neusoft.shopingapi.service.GoodsService;
import com.neusoft.shopingapi.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@CrossOrigin
@RestController
public class Goodscontroller {
   @Autowired
    private GoodsService service;
   @Autowired
   private PictureService pictureService;
    @GetMapping("/qureyPage")
    public Map<String,Object> qurerPage(Integer pageid){
        Page<GoodsEntity> page=new Page<>();
        if(null==pageid||pageid.intValue()==0)
        {
            page.setCurenPage(1);
        }else {
            page.setCurenPage(pageid);
        }
        List<GoodsEntity> goodsList = service.queryAll(page).getArrys();
        goodsList.forEach(goods->{
            List<String> images=pictureService.qureyFirstUrl(goods.getGoodsno());
           goods.setImages(images);
        });
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","分页查询成功!!!");
        map.put("data",goodsList);
        map.put("totalpage",page.getTotalpage());

        return map;
    }
    @PostMapping("/qureyGoods")
    public  Map<String,Object> qurerOne(@RequestBody GoodsEntity ingoods){
        Integer goodsno=ingoods.getGoodsno();
        if (goodsno==null){
            Map<String,Object> map = new ConcurrentHashMap<>();
            map.put("code",new Integer(2000000));
            map.put("msg","查询失败!!!");
            return map;
        }else {
            GoodsEntity goods=service.getGoodsByOne(goodsno);
            List<String> images=pictureService.qureyOneAllUrl(goodsno);
            goods.setImages(images);
            Map<String,Object> map = new ConcurrentHashMap<>();
            map.put("code",new Integer(100000));
            map.put("msg","查询成功!!!");
            map.put("data",goods);
            return map;
        }
    }
    @PostMapping("/qureyTypeGoods")
    public  Map<String,Object> qurerByType(@RequestBody TypeEntity type){
        Integer typeno=type.getTypeno();
        List<GoodsEntity> goodsList = service.getGoodsByType(typeno);
        goodsList.forEach(goods->{
            List<String> images=pictureService.qureyFirstUrl(goods.getGoodsno());
            goods.setImages(images);
        });
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","查询成功!!!");
        map.put("data",goodsList);
        return map;
    }
}
